<?php
/**
 * OrderSimple.php
 *
 * @copyright  2022 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     Edward Yang <yangjin@guangda.work>
 * @created    2022-08-24 10:51:53
 * @modified   2022-08-24 10:51:53
 */

namespace Beike\Admin\Http\Resources;

use Beike\Services\StateMachineService;
use Illuminate\Http\Resources\Json\JsonResource;

class OrderProductSimple extends JsonResource
{
    public function toArray($request): array
    {
        $data = [
            'id'            => $this->id,
            'number'        => $this->number,
            'customer_name' => $this->customer_name,
            'email'         => $this->email,
            'telephone'     => $this->telephone,
            'created_at'    => $this->created_at,
            'updated_at'    => $this->updated_at,
            'total_format'  => $this->getTotalFormatAttribute()??'',
            'status_format' => $this->getStatusFormatAttribute()??'',

            'product_id'    => $this->product_id,
            'name'          => $this->name,
            'image'         => image_resize($this->image),
            'quantity'      => $this->quantity,
            'price'         => currency_format($this->price),
        ];

        return $data;
    }


    public function getStatusFormatAttribute()
    {
        $statusMap = array_column(StateMachineService::getAllStatuses(), 'name', 'status');

        return $statusMap[$this->status];
    }

    public function getTotalFormatAttribute()
    {
        return currency_format($this->total, $this->currency_code, $this->currency_value);
    }

}
